layui.use(["layer", "form"], function () {
    var layer = layui.layer, form = layui.form, $ = layui.jquery;

    // 全局刷新
    if (window.top != window.self) {
        top.location.href = "main"
    }

    // 验证码点击
    $("#captchaImg").click(function () {
        $("#captchaImg").attr("src", "getCaptcha")
    });

    // 执行登录
    form.on("submit(login)", function (data) {
        // 禁用登录按钮
        $(this).text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        setTimeout(function () {
            $("#loginButton").text("登录").attr("disabled", false).removeClass("layui-disabled");
        }, 2000);
        // 密码加密
        data.field.password = md5(data.field.password);

        $.ajax({
            type: "post", url: "login", data: data.field, dataType: "json", success: function (res) {
                if (res.code == 200) {
                    window.location.href = "main";
                    return;
                }
                if (res.code == 202) {
                    $("#password").val("")
                }
                layer.msg(res.msg, {icon: 2});
                $("#captchaCode").val("");
                $("#captchaImg").trigger("click");
            }
        });
        return false;
    })
});